/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.invoice.model.dao;

import com.invoice.busines.dto.ComprobanteCampoDTO;
import com.invoice.busines.dto.ComprobanteDTO;
import com.invoice.busines.dto.ComprobanteDetalleDTO;
import com.invoice.exceptions.InvoiceException;
import com.invoice.model.entities.Comprobante;
import com.invoice.model.entities.ComprobanteCampo;
import com.invoice.model.entities.Comprobantedetalle;
import com.invoice.enums.TipoComprobante;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;

/**
 *
 * @author Lidia
 */
public class ComprobanteDAOHibernate  extends DAOGenerico implements ComprobanteDAO{
    ArticuloDAOHibernate articuloDAO = new ArticuloDAOHibernate();
    ClienteDAOHibernate clienteDAO = new ClienteDAOHibernate();
    VendedorDAOHibernate vendedorDAO = new VendedorDAOHibernate();
    
    @Override
    public Comprobante convertDTOtoPojo(Object object) {
        ComprobanteDTO comprobanteDTO = (ComprobanteDTO) object;
        Comprobante comprobante = new Comprobante();
        comprobante.setIdComprobante(comprobanteDTO.getIdComprobante());
        comprobante.setNumComp(comprobanteDTO.getNumComp());
        comprobante.setNumGuia(comprobanteDTO.getNumGuia());
        comprobante.setObservac(comprobanteDTO.getObservac());
        comprobante.setFecha(comprobanteDTO.getFecha());
        if(comprobanteDTO.getClienteDTO()!= null && comprobanteDTO.getVendedorDTO().getNombre() != null && !comprobanteDTO.getClienteDTO().getNombre().isEmpty()){
            comprobante.setClientes(clienteDAO.convertDTOtoPojo(comprobanteDTO.getClienteDTO()));
        }
        if(comprobanteDTO.getVendedorDTO()!= null && comprobanteDTO.getVendedorDTO().getNombre() != null && !comprobanteDTO.getVendedorDTO().getNombre().isEmpty()){
            comprobante.setVendedor(vendedorDAO.convertDTOtoPojo(comprobanteDTO.getVendedorDTO()));
        }
        comprobante.setDescuento(comprobanteDTO.getDescuento());
        comprobante.setSubTotal(comprobanteDTO.getSubTotal());        
        comprobante.setTotal(comprobanteDTO.getTotal());
        comprobante.setEstado(comprobanteDTO.getEstado());      
        comprobante.setTipoComprobante(comprobanteDTO.getTipoComprobante());
//        comprobante.setTipoComp(comprobanteDTO.getTipoComp());
        if(comprobanteDTO.getComprobantedetalles() != null){
            Comprobantedetalle comprobanteDetalle;
            for (ComprobanteDetalleDTO comprobanteDetalleDTO : comprobanteDTO.getComprobantedetalles()) {
                comprobanteDetalle = new Comprobantedetalle();
                comprobanteDetalle.setCant(comprobanteDetalleDTO.getCant());
                comprobanteDetalle.setArticulos(articuloDAO.convertDTOtoPojo(comprobanteDetalleDTO.getArticuloDTO()));
                comprobanteDetalle.setPvp(comprobanteDetalleDTO.getPvp());
                comprobanteDetalle.setIdDetalle(comprobanteDetalleDTO.getNumDet());
                comprobante.addComprobanteDetalle(comprobanteDetalle);
            }
        }
        if(comprobanteDTO.getComprobanteCampoDTOs() != null){
            ComprobanteCampo comprobanteCampo;
            for (ComprobanteCampoDTO comprobanteCampoDTO : comprobanteDTO.getComprobanteCampoDTOs()){
                comprobanteCampo = new ComprobanteCampo();
                comprobanteCampo.setIdComprobanteCampo(comprobanteCampoDTO.getIdComprobanteCampo());
                comprobanteCampo.setNameLabel(comprobanteCampoDTO.getNameLabel());
                comprobanteCampo.setValueLabel(comprobanteCampoDTO.getValueLabel());
                comprobante.addComprobanteCampo(comprobanteCampo);
            }
        }
        return comprobante;
    }

    @Override
    public ComprobanteDTO convertPojotoDTO(Object object) {
        ComprobanteDTO comprobanteDTO = new ComprobanteDTO();
        if (object == null) {
            return null;
        }
        try {
            Comprobante comprobante = (Comprobante) object;
            comprobanteDTO.setIdComprobante(comprobante.getIdComprobante());
            comprobanteDTO.setNumComp(comprobante.getNumComp());
            comprobanteDTO.setNumGuia(comprobante.getNumGuia());
            comprobanteDTO.setObservac(comprobante.getObservac());
            comprobanteDTO.setFecha(comprobante.getFecha());
            comprobanteDTO.setClienteDTO(clienteDAO.convertPojotoDTO(comprobante.getClientes()));
            comprobanteDTO.setVendedorDTO(vendedorDAO.convertPojotoDTO(comprobante.getVendedor()));
            comprobanteDTO.setDescuento(comprobante.getDescuento());
            comprobanteDTO.setTotIva(comprobante.getTotIva());
            comprobanteDTO.setSubTotal(comprobante.getSubTotal());        
            comprobanteDTO.setEstado(comprobante.getEstado());      
            comprobanteDTO.setTipoComprobante(comprobante.getTipoComprobante());
            comprobanteDTO.setTotal(comprobante.getTotal());
            if(comprobante.getComprobantedetalles() != null){
                ComprobanteDetalleDTO comprobanteDetalleDTO;
                for (Comprobantedetalle comprobanteDetalle : comprobante.getComprobantedetalles()) {
                    comprobanteDetalleDTO = new ComprobanteDetalleDTO();
                    comprobanteDetalleDTO.setCant(comprobanteDetalle.getCant());
                    comprobanteDetalleDTO.setArticuloDTO(articuloDAO.convertPojotoDTO(comprobanteDetalle.getArticulos()));
                    comprobanteDetalleDTO.setPvp(comprobanteDetalle.getPvp());
                    comprobanteDetalleDTO.setNumDet(comprobanteDetalle.getIdDetalle());
                    comprobanteDTO.addComprobantedetalle(comprobanteDetalleDTO);
                }
            }
            if(comprobante.getComprobanteCampos() != null){
                ComprobanteCampoDTO comprobanteCampoDTO;
                for (ComprobanteCampo comprobanteCampo : comprobante.getComprobanteCampos()){
                    comprobanteCampoDTO = new ComprobanteCampoDTO();
                    comprobanteCampoDTO.setIdComprobanteCampo(comprobanteCampo.getIdComprobanteCampo());
                    comprobanteCampoDTO.setNameLabel(comprobanteCampo.getNameLabel());
                    comprobanteCampoDTO.setValueLabel(comprobanteCampo.getValueLabel());
                    comprobanteDTO.addComprobanteCampoDTO(comprobanteCampoDTO);
                }
            }
        } catch (Exception e) {
            return null;
        }
        return comprobanteDTO;
    }

    @Override
    public List<ComprobanteDTO> findAll() throws InvoiceException {
        List<ComprobanteDTO> result = new ArrayList<ComprobanteDTO>();
        try{
            startSession();
            Query query=session.createQuery("from Comprobante");
            List<Comprobante> list=query.list();
            for (Comprobante comprobante : list) {
                ComprobanteDTO comprobanteDTO=convertPojotoDTO(comprobante);
                result.add(comprobanteDTO);
            }            
            transaction.commit();
            return result;
        } catch (Exception e) {
            rollback();
            throw new InvoiceException("Error en busqueda", e);
        }finally{
            closeSession();
        }
    }

    @Override
    public List<ComprobanteDTO> findGroupByFecha() throws InvoiceException {
        List<ComprobanteDTO> result = new ArrayList<ComprobanteDTO>();
        try{
            startSession();
            Query query=session.createQuery("select sum(total), fecha from Comprobante group by fecha");
            List list=query.list();
            for (Object obj : list) {
                Object [] o = (Object[])obj;
                ComprobanteDTO comprobanteDTO= new ComprobanteDTO();
                comprobanteDTO.setTotal((BigDecimal)o[0]);
                comprobanteDTO.setFecha((Date)o[1]);
                result.add(comprobanteDTO);
            }            
            transaction.commit();
            return result;
        } catch (Exception e) {
            rollback();
            throw new InvoiceException("Error en busqueda", e);
        }finally{
            closeSession();
        }
    }
    @Override
    public BigDecimal findTotalGroupByFecha(Date fecha) throws InvoiceException {
        BigDecimal result;
        try{
            startSession();
            Query query=session.createQuery("select sum(total) from Comprobante where fecha=:fecha group by fecha").setParameter("fecha", fecha);
            Object obj = query.uniqueResult();
            result = (BigDecimal)obj;
            transaction.commit();
            return result;
        } catch (Exception e) {
            rollback();
            throw new InvoiceException("Error en busqueda", e);
        }finally{
            closeSession();
        }
    }
    @Override
    public List<TipoComprobante> findTiposComprobante() throws InvoiceException {
        List<TipoComprobante> result = new ArrayList<TipoComprobante>();
        try{
            startSession();
            Query query=session.createQuery("from Tipocomprobante");
            result = query.list();
            transaction.commit();
        } catch (Exception e) {
            rollback();
            throw new InvoiceException("Error en busqueda", e);
        }finally{
            closeSession();
        }
        return result;
    }
}
